{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 42 Web爬取"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.【填空】请写出用Scrapy Shell从HTML源码中提取出title元素的代码（ ）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：response.css(\".title::text\").extract()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.【填空】请写出用Scrapy Shell从XML源码中提取出title元素的代码（ ）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：response.xpath(\"//item/title\").extract_first()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.【判断】判断下面创建Scrapy项目的方式是否正确：（ ）   \n",
    "在Scrapy Shell中用代码 scrapy startproject firstone创建一个新的Scrapy项目。  \n",
    "\n",
    "A.√  \n",
    "B.×"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.【填空】根据注释，在横线处写出合适的代码：（ ）（ ）  \n",
    "\n",
    "`def parse(self,response):\n",
    "    #使用 css 采集器提取内容  \n",
    "    titles = response.css('.title.may-blank::text').extract()\n",
    "    votes = response.css('.score.unvoted::text').extract()\n",
    "    times = response.css('time::attr(title)').extract()\n",
    "    comments = response.css('.comments::text').extract()\n",
    "    #将提取的内容按行排序\n",
    "    ________________\n",
    "       #创建字典，存储爬取的信息\n",
    "       scraped_info = {\n",
    "          'title': item[0],\n",
    "          'vote': item[1],\n",
    "          'created_at': item[2],\n",
    "          'comments': item[3],\n",
    "       }\n",
    "       #将爬取的信息传递给Scrapy\n",
    "       ________________`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：   \n",
    "for item in zip(titles, votes, times, comments):  \n",
    "yield scraped_info"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.【编程】使用Scrapy对当当网上有关《数据科学》的书的书名、作者、出版时间等数据进行爬取。   \n",
    "URL为http://search.dangdang.com/?key=%CA%FD%BE%DD%BF%C6%D1%A7&act=input"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：参见本章内容。"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
